package top.v5it.japi.plus.common.tuple;

import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 一元组
 *
 * @author zhanpu
 * @date 2022/6/18
 */
@Getter
@EqualsAndHashCode(callSuper = false)
public class Tuple1<T1> implements Iterable<Object>, Serializable {

    private static final long serialVersionUID = -3455967223034805485L;
    final T1 one;

    Tuple1(T1 one) {
        this.one = one;
    }

    public Object[] toArray() {
        return new Object[]{this.one};
    }

    public List<Object> toList() {
        return Arrays.asList(this.toArray());
    }

    @Override
    public Iterator<Object> iterator() {
        return toList().iterator();
    }

    public int size() {
        return 1;
    }

    public Object get(int index) {
        return index == 0 ? this.one : null;
    }

    @Override
    public final String toString() {
        return Tuples.tupleStringRepresentation(this.toArray()).insert(0, '[').append(']').toString();
    }
}
